package com.yeban.javase.day17.test.test3;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/31
 */
public class Account {
    private String name;
    private double balance;

    // 存款
    public void deposit(double money) {
        synchronized (this) {
            while(balance >= 10000) {
                System.out.println(Thread.currentThread().getName() + "：生活费够了！不打钱了！" + " 还有：" + balance + "元");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            balance += money;
            System.out.println(Thread.currentThread().getName() + "：打上生活费了！钱包又瘪了！" + " 还有：" + balance + "元");
            this.notifyAll();
        }
    }

    // 取款
    public void withdraw(double money) {
        synchronized (this) {
            while (balance < money) {
                System.out.println(Thread.currentThread().getName() + "：等待打生活费！孩子要饿死了！" + " 还有：" + balance + "元");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            balance -= money;
            System.out.println(Thread.currentThread().getName() + "：拿到生活费了！孩子又活了！" + " 还有：" + balance + "元");
            this.notifyAll();
        }
    }


    public Account() {
    }

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * 设置
     * @param balance
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String toString() {
        return "Account{name = " + name + ", balance = " + balance + "}";
    }
}
